package com.example.xinyun.provider;

import cn.droidlover.xrecyclerview.XRecyclerView;
import com.bumptech.glide.Glide;
import com.example.xinyun.MyApplication;
import com.example.xinyun.ResourceTable;
import com.example.xinyun.db.TbTz;
import com.example.xinyun.domain.convert;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import java.util.Base64;
import java.util.List;

public class TzOwnProvider<T> extends XRecyclerView.RecyclerAdapter<TbTz> {

    private List<TbTz> settingList;
    private Context context;

    public TzOwnProvider(Context context, List<TbTz> list) {
        super(context, list);
        this.settingList=list;
        this.context = context;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        SettingHolder holder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_own_item_lun, null, false);

            holder = new SettingHolder(cpt);
            // 将获取到的子组件信息绑定到列表项的实例中
            cpt.setTag(holder);

        } else {
            cpt = component;
            // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
            holder = (SettingHolder) cpt.getTag();
        }

        holder.mt1.setText(settingList.get(i).getByuser());
        PixelMap pixelMap = convert.ByteToPixelMap(MyApplication.user_Image);
        holder.userimage.setPixelMap(pixelMap);
        holder.mt5.setText(settingList.get(i).getTzcontent());
        holder.mt2.setText("发布时间"+settingList.get(i).getBytime());
        holder.mt6.setText("帖子："+settingList.get(i).getTztitle());
        holder.dznum.setText("（"+settingList.get(i).getDizan()+")");
        holder.mt4.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder().withDeviceId("")
                        .withBundleName("com.xijing.xinyun")
                        .withAbilityName("com.example.xinyun.TzDatilsAbility")
                        .build();
                intent.setParam("id",settingList.get(i).getId());
                intent.setParam("isown","1");
                intent.setOperation(operation);
                context.startAbility(intent,0);
            }
        });
        holder.mt3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ToastDialog(context)
                        .setText("自己不能给自己点赞")
                        .show();
            }
        });
        if(settingList.get(i).getTzimg()!=null){

            byte[] str = Base64.getDecoder().decode(settingList.get(i).getTzimg());
            Glide.with(context).load(Base64.getDecoder().decode(str)).into(holder.mimg);
        }

        return cpt;

    }

    private static class SettingHolder {
        Image userimage;
        Text mt1;
        Text mt2;
        Text mt5;
        Text mt6;
        Text mtdz;
        DirectionalLayout mt3;
        DirectionalLayout mt4;
        Text dznum;
        Image mimg;

        SettingHolder(Component component) {
            userimage=(Image) component.findComponentById(ResourceTable.Id_userimage);
            userimage.setCornerRadius(10f);
            mt1 = (Text) component.findComponentById(ResourceTable.Id_t1);
            mt2 = (Text) component.findComponentById(ResourceTable.Id_t2);
            mt3 = (DirectionalLayout) component.findComponentById(ResourceTable.Id_t3);
            mt4 = (DirectionalLayout) component.findComponentById(ResourceTable.Id_t4);
            mt5 = (Text) component.findComponentById(ResourceTable.Id_t5);
            mt6 = (Text) component.findComponentById(ResourceTable.Id_t6);
            mimg = (Image) component.findComponentById(ResourceTable.Id_img);
            dznum = (Text) component.findComponentById(ResourceTable.Id_dznum);
        }
    }
}
